﻿using System;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using zTalk.Network;

namespace zTalk
{
    public partial class Login : Window
    {
        private static bool inited = false;

        public Login()
        {
            InitializeComponent();

            App.Handler.Login += new PacketEventHandler(handler_Login);
            App.Handler.GetFriends += new PacketEventHandler(handler_GetFriends);
        }

        private void handler_GetFriends(object sender, PacketEventArgs ea)
        {
            Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate {
                if (inited == false)
                {
                    inited = true;
                    Contacts c = new Contacts();
                    c.UserItems = new ObservableCollection<UserItem>((UserItem[])ea.Result);
                    c.Show();
                    App.Current.Windows[0].Hide();
                    App.Current.Windows[0].Close();
                }
            }));
        }

        private void handler_Login(object sender, PacketEventArgs ea)
        {
            if (ea.Result != null)
            {
                App.User = (UserItem)ea.Result;
                Packet p = Packet.Create(Opcodes.GET_FRIENDS, "");
                App.Client.SendPacket(p);
            }
            else
            {
                triggerLoading(false);
                Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate {
                    lblErrors.Content = "! Invalid username or password";
                    errorPane.Visibility = Visibility.Visible;
                }));
            }
        }

        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                triggerLoading(true);

                if (App.Client.Running == false)
                {
                    App.Client.Start();
                }

                string[] auth = { txtUsername.Text, App.Sha1(txtPassword.Password) };
                Packet p = Packet.Create(Opcodes.LOGIN, Json.Encode<string[]>(auth));
                App.Client.SendPacket(p);
            }
            catch
            {
                triggerLoading(false);
                lblErrors.Content = "! Cannot connect to server";
                errorPane.Visibility = Visibility.Visible;
            }
        }

        private void triggerLoading(bool start)
        {
            Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate {
                btnLogin.Content = (start == true) ? "Processing" : "Sign in";
                btnLogin.IsEnabled = (start != true);
            }));
        }

        private void lblRegister_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Register r = new Register();
            r.Show();
            Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            txtUsername.Focus();
        }

        public void InitFields(string username, string password)
        {
            txtUsername.Text = username;
            txtPassword.Password = password;
        }
    }
}
